﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWeb.Models;
using System.Text;
using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
using SailHero.Soft.FrameWork.Models;
using MvcWeb.Helper;
namespace MvcWeb.Controllers
{
    [Export(typeof(IController)), ExportMetadata("Name", "Role")]
    public class RoleController : BaseController
    {

        /// <summary>
        /// 数据操作接口
        /// </summary>
        [Import]
        public IRoleRepository roleModel;

        [Import]
        public IModelRepository modModel;

        [Import]
        public IUserInRoleRepository UinRModel;

        [Import]
        public ISys_UserRepository userModel;


        [Authorize]
        public ActionResult Index()
        {
            var roleList = roleModel.GetAllList();
            return View(roleList);
        }

        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize]
        public string Create2()
        {
            try
            {
                SYS_ROLE role = new SYS_ROLE()
                {
                    ROLEID = DateTime.Now.Ticks.ToString(),
                    ROLENAME = HttpUtility.UrlDecode(Request.Form["RoleName"]),
                    REMARK = HttpUtility.UrlDecode(Request.Form["Remark"]),
                    CREATOR = LoginUser.ACCOUNT,
                    CREATETIME = DateTime.Now
                };
                roleModel.Add(role);
                roleModel.UnitOfWork.SaveChanges();
                return "success";

            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        [Authorize]
        public ActionResult Edit(string id)
        {
            var role = roleModel.Get(id);
            return View(role);
        }

        [Authorize]
        public string Edit2(string id)
        {
            try
            {
                var role = roleModel.Get(id);
                role.ROLENAME = HttpUtility.UrlDecode(Request.Form["RoleName"]);
                role.REMARK = HttpUtility.UrlDecode(Request.Form["Remark"]);
                roleModel.UnitOfWork.SaveChanges();

                return "success";

            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        [Authorize]
        public string Delete(string id)
        {
            try
            {
                // TODO: Add delete logic here
                roleModel.Delete(itm => itm.ROLEID == id);
                roleModel.UnitOfWork.SaveChanges();
                return "success";
            }
            catch
            {
                return "fail";
            }
        }

        [Authorize]
        public ActionResult UserSet(string id)
        {
            var role = roleModel.Get(id);

            var userList = userModel.GetAllList();
            var list = UinRModel.GetList(itm => itm.ROLEID == id);
            StringBuilder sbUser = new StringBuilder();
            foreach (SYS_USERINROLE mod in list)
            {
                sbUser.Append(mod.USERID + ",");
            }
            ViewData["userList"] = new SelectList(userList, "USERID", "USERCNAME", sbUser);
            ViewBag.Helper = new CheckBoxListHelper();
            return View(role);
        }

        [Authorize]
        public string UserSet2(string userID, string roleID)
        {
            return roleModel.SetUserRole(userID, roleID);
        }

        [Authorize]
        public ActionResult AuthorizationSet(string id)
        {
            var role = roleModel.FirstOrDefault(itm => itm.ROLEID == id);
            List<SYS_MODEL> modelList = modModel.GetAllList().ToList();
            ViewData["modelList"] = modelList.Where(model => model.ISSHOW == "1").ToList();

            var permissionList = roleModel.GetPermissionListByRole(id);
            StringBuilder sbPermission = new StringBuilder();
            foreach (SYS_ROLEPERMISSION mod in permissionList)
            {
                sbPermission.Append(mod.MODELID + ",");
            }
            ViewData["PermissionList"] = sbPermission.ToString();
            ViewBag.Helper =new CheckBoxListHelper();
            return View(role);
        }

        [Authorize]
        public string AuthorizationSet2(string roleID, string parentID, string subID)
        {
            return roleModel.SetPermission(roleID, parentID, subID);
        }
    }
}
